package com.myway.components.ehic.util;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.ws.rs.core.HttpHeaders;

import com.myway.components.ehic.model.YHConfig;
import org.apache.log4j.Logger;

import com.myway.components.ehic.support.Params;
import com.myway.components.ehic.support.RequestCommon;
import com.myway.components.ehic.support.RequestParam;
import com.myway.components.ehic.support.ResponseParam;
import com.myway.core.spring.SpringBeanUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 类名称：RequestClientUtil <br/>
 * 类描述：请求方式 <br/>
 * 创建时间：2016年3月3日 下午2:10:21 <br/>
 * 
 * @author 张涛
 * @version V1.0
 */
public class RequestClient {
	private static final Logger log = Logger.getLogger(RequestClient.class);
	private static final Gson GSON = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
	private static final String METHOD = "GET";
	private static final String CONTENT_TYPE = "application/json";

	public static YHConfig yhConfig = (YHConfig) SpringBeanUtil.lookup("YHConfig");
	@SuppressWarnings("rawtypes")
	public static String post(String requestUri, RequestParam rqp) {
		try {
			RequestCommon rc = new RequestCommon(rqp);
			String json = GSON.toJson(rc);
            System.out.println("请求明文:"+json);
			String requestParam = getRequestParam(json);
			System.out.println("请求密文:"+requestParam);
//			String authorization = getAuthorization(json);
			String authorization = getAuthorization2(json);
			System.out.println("请求authorization:"+authorization);
//			System.out.println("请求authorization2:"+authorization2);
			log.info("request url:"+yhConfig.getRequestUrl()+requestUri);
			URL url = new URL(yhConfig.getRequestUrl() + requestUri);
			HttpURLConnection connection = null;
			connection = (HttpURLConnection) url.openConnection();
			connection.setDoInput(true);
			connection.setDoOutput(true);
			connection.setRequestMethod(METHOD);
			connection.setUseCaches(false);
			connection.setRequestProperty(HttpHeaders.AUTHORIZATION,authorization);
			connection.setRequestProperty("Source", yhConfig.getSource());
			connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE);
			connection.connect();
			DataOutputStream dataout = new DataOutputStream(connection.getOutputStream());
			dataout.writeBytes(requestParam);
			dataout.flush();
			dataout.close();
			System.out.println(connection.getResponseCode());
			InputStream in = connection.getInputStream();
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int i;
			while ((i = in.read(b)) != -1)
				bos.write(b, 0, i);
			in.close();
			String resultStr = new String(bos.toByteArray(), "utf-8");
			System.out.println("response:"+resultStr);
			ResponseParam resultParam = GSON.fromJson(resultStr, ResponseParam.class);
			String result = null;
			if(null!=resultParam&&resultParam.isIsSuccess()){
				result = GSON.toJson(resultParam.getResult());
			}else{
//				throw new T ransferAirportException(resultParam.getMessage());
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static String getAuthorization(String json) {
		String json_ = yhConfig.getMd5Key() + json;
		return MD5Util.string2MD5(json_);
	}
	
	public static String getAuthorization2(String json) {
		String json_ = yhConfig.getMd5Key() + json;
		return MD5Util.MD5(json_, "utf-8");
	}
	
	public static String getRequestParam(String json) throws Exception{
		String param = null;
		try {
			String encrypt = DESSignUtil.encrypt(json, yhConfig.getDesKey());
			Params params = new Params(encrypt);
			param = GSON.toJson(params);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("MD5加密失败!");
		}
		return param;
	}
	
	
	
	
}
